package com.xsdl.giteespringboottest.controller;

import cn.hutool.core.map.MapUtil;
import com.xsdl.giteespringbootautoconfigure.config.GiteeProperty;
import com.xsdl.giteespringboottest.dto.UserDTO;
import com.xsdl.giteespringboottest.service.GiteeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/gitee")
@Slf4j
public class GiteeController {

    @Autowired
    private GiteeProperty giteeProperty;

    @Autowired
    private GiteeService giteeService;

    @GetMapping("/config")
    public Map<String, String> getGiteeConfig() {
        Map<String, String> result = new HashMap<>();
        result.put("clientId", giteeProperty.getClientId());
        result.put("redirectUri", giteeProperty.getRedirectUri());
        return result;
    }

    @PostMapping("/login")
    public UserDTO giteeLogin(@RequestBody Map<String, String> body) {
        String code = MapUtil.getStr(body, "code");
        if (StringUtils.isEmpty(code)) {
            log.error("没有收到授权码");
            return null;
        }
        return giteeService.login(code);
    }

}
